package com.zz.load.handle;

import com.zz.load.exception.MyServletException;
import com.zz.load.servlet.MyServlet;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class MyServletMap {
    public static ConcurrentHashMap<String, Object> pathMap = new ConcurrentHashMap<>();

    static {
        try {
            Properties pro = new Properties();
            pro.load(new FileInputStream("web.properties"));
            Set<String> stringSet = pro.stringPropertyNames();
            for (String key : stringSet) {
                String value = pro.getProperty(key);
                Object obj = Class.forName(value).newInstance();
                if(obj instanceof MyServlet){
                    pathMap.put(key, obj);
                }else{
                    throw new MyServletException("404 NOT Found");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (MyServletException e) {
            e.printStackTrace();
        }
    }
}
